<template>
  <div class="loginBox">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item prop="userName">
        <el-input v-model="ruleForm.userName" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          suffix-icon="el-icon-view"
          placeholder="密码"
          v-model="ruleForm.password"
          prefix-icon="myicon myicon-key"
        ></el-input>
      </el-form-item>

      <div class="drag" ref="dragDiv">
        <div class="drag_bg"></div>
        <div class="drag_text">{{ confirmWords }}</div>
        <div
          ref="moveDiv"
          @mousedown="mousedownFn($event)"
          :class="{ handler_ok_bg: confirmSuccess }"
          class="handler handler_bg"
          style="position: absolute; top: 0px; left: 0px"
        ></div>
      </div>

      <el-form-item class="button">
        <el-button type="primary" @click="submitForm('ruleForm')"
          >登录</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
      <h6>Copyright© 2012-2021-管理系统-Version V1.1</h6>
    </el-form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      ruleForm: {
        userName: "",
        password: "",
        token: ""
      },

      // 滑块验证
      beginClientX: 0 /* 距离屏幕左端距离 */,
      mouseMoveStata: false /* 触发拖动状态  判断 */,
      maxwidth: "" /* 拖动最大宽度，依据滑块宽度算出来的 */,
      confirmWords: "拖动滑块验证" /* 滑块文字 */,
      confirmSuccess: false /* 验证成功判断 */,

      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    async submitForm (formName) {
      if (this.confirmSuccess == true) {
        try {
          let success = await this.$refs[formName].validate();
          if (!success) return false;

          let {
            data: {
              user: { userName },
              token
            },
            meta: { status },
          } = await this.$http.getLogin(this.ruleForm);
          console.log(token);
          if (status == 200) {
            this.$message.success("登录成功");
            this.$store.commit("setToken", token); //vuex
            this.$store.commit("setUsername", userName); //vuex

            // 跳转到home 页面
            this.$router.push({ path: "/branchDynamics" });
          }
        } catch (error) {
          console.log(error);
          this.Reset()
        }
      } else {
        this.$message("请验证");
      }
    },
    //重置
    resetForm (formName) {
      this.Reset()
      this.$refs[formName].resetFields();
    },

    // 重置滑块
    Reset () {
      this.beginClientX = 0 /* 距离屏幕左端距离 */
      this.mouseMoveStata = false /* 触发拖动状态  判断 */
      this.maxwidth = 0 /* 拖动最大宽度，依据滑块宽度算出来的 */
      this.confirmWords = "拖动滑块验证" /* 滑块文字 */
      this.confirmSuccess = false /* 验证成功判断 */
      document.getElementsByClassName("handler")[0].style.left = 0 + "px";
      document.getElementsByClassName("drag_bg")[0].style.width = 0 + "px";
      document.getElementsByClassName("drag_text")[0].style.color = "#000";
      this.maxwidth =
        this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth;
      document
        .getElementsByTagName("html")[0]
        .addEventListener("mousemove", this.mouseMoveFn);
      document
        .getElementsByTagName("html")[0]
        .addEventListener("mouseup", this.moseUpFn);
    },

    // 滑块验证
    mousedownFn: function (e) {
      if (!this.confirmSuccess) {
        e.preventDefault && e.preventDefault(); // 阻止文字选中等 浏览器默认事件
        this.mouseMoveStata = true;
        this.beginClientX = e.clientX;
      }
      console.log(this.confirmSuccess);
    }, // mousedoen 事件
    successFunction () {
      this.confirmSuccess = true;
      this.confirmWords = "验证通过";
      if (window.addEventListener) {
        document
          .getElementsByTagName("html")[0]
          .removeEventListener("mousemove", this.mouseMoveFn);
        document
          .getElementsByTagName("html")[0]
          .removeEventListener("mouseup", this.moseUpFn);
      } else {
        document
          .getElementsByTagName("html")[0]
          .removeEventListener("mouseup", () => { });
      }
      document.getElementsByClassName("drag_text")[0].style.color = "#fff";
      document.getElementsByClassName("handler")[0].style.left =
        this.maxwidth + "px";
      document.getElementsByClassName("drag_bg")[0].style.width =
        this.maxwidth + "px";
    }, // 验证成功函数
    mouseMoveFn (e) {
      if (this.mouseMoveStata) {
        let width = e.clientX - this.beginClientX;
        if (width > 0 && width <= this.maxwidth) {
          document.getElementsByClassName("handler")[0].style.left =
            width + "px";
        } else if (width > this.maxwidth) {
          this.successFunction();
        }
      }
    }, // mousemove事件
    moseUpFn (e) {
      console.log(1)
      this.mouseMoveStata = false;
      var width = e.clientX - this.beginClientX;
      if (width < this.maxwidth) {
        document.getElementsByClassName("handler")[0].style.left = 0 + "px";
        document.getElementsByClassName("drag_bg")[0].style.width = 0 + "px";
      }
    }, // mouseup事件
  },
  mounted () {
    this.maxwidth =
      this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth;
    document
      .getElementsByTagName("html")[0]
      .addEventListener("mousemove", this.mouseMoveFn);
    document
      .getElementsByTagName("html")[0]
      .addEventListener("mouseup", this.moseUpFn);
  },
};
</script>
  
<style scoped lang="less">
.loginBox {
  padding: 20px;
  padding-bottom: 0;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(153, 153, 153, 0.3);
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .el-form-item {
    margin: 0;
    /deep/ .el-form-item__content {
      width: 360px;
      height: 40px;
      margin: 0 !important;
      margin-bottom: 30px !important;
      .el-button {
        width: 175px;
        height: 40px;
      }
    }
  }
  .button {
    /deep/ .el-form-item__content {
      margin: 0 !important;
    }
  }
  h6 {
    text-align: center;
    margin: 25px 0;
  }
}

.drag {
  position: relative;
  background-color: #ffcccc;
  width: 360px;
  height: 40px;
  line-height: 34px;
  text-align: center;
  margin-bottom: 15px;
}
.handler {
  width: 40px;
  height: 38px;
  border: 1px solid #ccc;
  cursor: move;
}
.handler_bg {
  background: #fff
    url("")
    no-repeat center;
}
.handler_ok_bg {
  background: #fff
    url("")
    no-repeat center;
}
.drag_bg {
  background-color: #7ac23c;
  height: 40px;
  width: 0px;
}
.drag_text {
  position: absolute;
  top: 0px;
  width: 100%;
  height: 40px;
  text-align: center;
  line-height: 40px;
  user-select: none;
}
</style>
